/*
 * TeleportManager.java
 *
 * Created on Torek, 23 september 2008, 22:54
 */

package leveleditor;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author  jernej
 */
public class TeleportManager extends JDialog {
    
    class ComboCellItem {
        
        private String name = null;
        private Point dest = null;
        private Point src = null;
        
        public ComboCellItem(Point src,Point dest){
            this.name = dest.x + ", " + dest.y;
            this.dest = dest;
            this.src = src;
        }
        
        @Override
        public String toString(){
            return name;
        }
        
        @Override
        public boolean equals(Object obj){
            if(obj instanceof ComboCellItem){
                ComboCellItem ccItem = (ComboCellItem) obj;
                if(this.dest.equals(ccItem.dest))
                    return true;
                else
                    return false;
            }
            return false;
        }
        
        public Point getSrcPoint(){
            return src;
        }
        
        public Point getDestPoint(){
            return dest;
        }

        @Override
        public int hashCode() {
            int hash = 3;
            hash = 37 * hash + (this.name != null ? this.name.hashCode() : 0);
            hash = 37 * hash + (this.dest != null ? this.dest.hashCode() : 0);
            return hash;
        }
    }

    private ArrayList<TeleportTranslation> telMap = null;
    private ArrayList<TeleportTranslation> telMapBackup = null;
    private ArrayList<Point> telList = null;
    /** Creates new form TeleportManager */
    public TeleportManager(ArrayList<TeleportTranslation> mapping, ArrayList<Point> list) {
        setModalityType(ModalityType.APPLICATION_MODAL);
        initComponents();
        telList = list;
        telMap = mapping;
        telMapBackup = new ArrayList<TeleportTranslation>(telMap);
        this.setPreferredSize(new Dimension(250,30*(telList.size()+1)));
        setLayout(new GridLayout(telList.size()+1,2,10,5));
        
        for(int i=0;i<telList.size();i++){
            Point curCell = telList.get(i);
            this.getContentPane().add(new JLabel(curCell.x + ", " + curCell.y));
            
            JComboBox combo = new JComboBox();
            for(int j=0;j<telList.size();j++)
                if(!telList.get(j).equals(curCell))
                    combo.addItem(new ComboCellItem(curCell,telList.get(j)));
           
            boolean isFound = false;
            for(int j=0;j<telMap.size();j++)
                if(telMap.get(j).src.equals(curCell)){
                    combo.setSelectedItem(new ComboCellItem(curCell,telMap.get(j).dest));
                    isFound = true;
                }
            if(!isFound){
                Point dest = ((ComboCellItem)combo.getSelectedItem()).getDestPoint();
                telMapBackup.add(new TeleportTranslation(curCell, dest));
            }
            
            combo.addItemListener(new ItemListener() {
                public void itemStateChanged(ItemEvent evt) {
                    if(!(evt.getSource() instanceof JComboBox))
                        return;
                    JComboBox combo =  (JComboBox) evt.getSource();
                    ComboCellItem ccItem = (ComboCellItem) combo.getSelectedItem();
                    for(int i=0;i<telMapBackup.size();i++)
                        if(telMapBackup.get(i).src.equals(ccItem.getSrcPoint()))
                            telMapBackup.get(i).dest = ccItem.getDestPoint();
                }
            });
            this.getContentPane().add(combo);
        }
        JButton btnOK = new JButton("OK");
        btnOK.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                Close(true);
            }
        });
        JButton btnCancel = new JButton("Prekliči");
        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                Close(false);
            }
        });
        this.getContentPane().add(btnOK);
        this.getContentPane().add(btnCancel);
    }
    
    private void Close(boolean  save){
        if(save){
            telMap.clear();
            telMap.addAll(telMapBackup);
        }
        this.setVisible(false);
        this.dispose();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.JDialog.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 159, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 151, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
